package com.hxxy.wyp.petsLover.common.rest.base.backend;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.hxxy.wyp.petsLover.RestBase;
import com.hxxy.wyp.petsLover.common.entities.HomeInfo;
import com.hxxy.wyp.petsLover.common.entities.News;
import com.hxxy.wyp.petsLover.common.entities.vo.req.HomeInfoReq;
import com.hxxy.wyp.petsLover.common.entities.vo.req.NewsReq;
import com.hxxy.wyp.petsLover.common.entities.vo.resp.HomeInfoVo;
import com.hxxy.wyp.petsLover.common.mapper.HomeInfoMapper;
import com.hxxy.wyp.petsLover.common.services.base.backend.HomeInfoService;
import com.hxxy.wyp.petsLover.common.services.base.backend.NewsService;
import com.hxxy.wyp.petsLover.config.RedisConfig;
import com.hxxy.wyp.petsLover.exception.AppException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @ClassName HomeInfoRest
 * @Description TODO
 * @Author wyp
 * @Date 2023/1/11 14:14
 * @Version 1.0
 */
@RestController
@ResponseBody
@RequestMapping("/home")
public class HomeInfoRest extends RestBase {

    @Autowired
    private HomeInfoService homeInfoService;

    @Autowired
    private NewsService newsService;

    @Autowired
    private RedisConfig redisConfig;


    @RequestMapping("all")
//    @SaCheckLogin
//    @SaCheckRole(value = {"other_admin","super_admin"}, mode = SaMode.OR)
//    @SaCheckPermission(value = {"all", "other_load"}, mode = SaMode.OR)
    public Object findAllInfo() {
        HomeInfoVo homeInfoVo = homeInfoService.findAllInfo();
        return ajaxSuccess(homeInfoVo, "200");
    }

    /**
     * 门户轮播图编辑操作
     *
     * @param fileList
     * @return
     */
    @RequestMapping("updatePic")
    @SaCheckLogin
    @SaCheckRole(value = {"other_admin","super_admin"}, mode = SaMode.OR)
    @SaCheckPermission(value = {"all", "other_update"}, mode = cn.dev33.satoken.annotation.SaMode.OR)
    public Object updatePicById(@RequestBody List<HomeInfoReq> fileList) {
        HomeInfoVo homeInfoVo = homeInfoService.updateBy(fileList);
        return ajaxSuccess(homeInfoVo, "200");
    }

    /**
     * 新闻管理的增删改操作
     *
     * @param id
     * @return
     */

    @RequestMapping("deleteOneNews")
    @SaCheckLogin
    @SaCheckRole(value = {"other_admin","super_admin"}, mode = SaMode.OR)
    @SaCheckPermission(value = {"all", "other_delete"}, mode = cn.dev33.satoken.annotation.SaMode.OR)
    public Object deleteOneNews(@RequestParam("id") String id) {
        boolean save = newsService.removeById(id);
        redisConfig.getRedisTemplateByDb(0).expire("homeInfo", Duration.ZERO);
        if (save) {
            return ajaxSuccess("200", "删除成功!");
        }
        return ajaxError("500", "删除失败!");
    }


    @RequestMapping("selectNewsFrontId")
    @SaCheckLogin
    @SaCheckRole(value = {"other_admin","super_admin"}, mode = SaMode.OR)
    @SaCheckPermission(value = {"all", "other_load"}, mode = cn.dev33.satoken.annotation.SaMode.OR)
    public Object selectNewsFrontId() {
        LambdaQueryWrapper<News> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(News::getNewsIsSelected, "1");
        List<News> list = newsService.list(lambdaQueryWrapper);
        return ajaxSuccess(list, "200");
    }

    @RequestMapping("updateNewsStatus")
    @SaCheckLogin
    @SaCheckRole(value = {"other_admin","super_admin"}, mode = SaMode.OR)
    @SaCheckPermission(value = {"all", "other_load"}, mode = cn.dev33.satoken.annotation.SaMode.OR)
    public Object updateNewsStatus(@RequestBody List<String> value2) {
        LambdaUpdateWrapper<News> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(News::getNewsIsSelected, "1").set(News::getNewsIsSelected, "0");
        boolean update = newsService.update(lambdaUpdateWrapper);
        for (String s : value2) {
            News news = new News();
            news.setId(Long.parseLong(s));
            news.setNewsIsSelected("1");
            newsService.updateById(news);
        }
        return ajaxSuccess("200", "修改成功!");
    }

    @RequestMapping("addNews")
    @SaCheckLogin
    @SaCheckRole(value = {"other_admin","super_admin"}, mode = SaMode.OR)
    @SaCheckPermission(value = {"all", "other_update"}, mode = cn.dev33.satoken.annotation.SaMode.OR)
    public Object addNews(@RequestBody NewsReq newsReq) {
        News news = new News();
        BeanUtils.copyProperties(newsReq, news);
        if (newsReq.getFileList().size()<1) {
            throw new AppException("500","请上传新闻图片!");
        }
        String pic = "";
        for (HomeInfoReq homeInfoReq : newsReq.getFileList()) {
            pic = pic + homeInfoReq.getUrl() + ",";
        }
        news.setNewsPic(pic);
        boolean b = newsService.save(news);
        redisConfig.getRedisTemplateByDb(0).expire("homeInfo", Duration.ZERO);
        if (b) {
            return ajaxSuccess("200", "新增成功!");
        }
        return ajaxError("新增失败!");
    }

    @RequestMapping("updateNews")
    @SaCheckLogin
    @SaCheckRole(value = {"other_admin","super_admin"}, mode = SaMode.OR)
    @SaCheckPermission(value = {"all", "other_update"}, mode = cn.dev33.satoken.annotation.SaMode.OR)
    public Object updateNews(@RequestBody NewsReq newsReq) {
        News news = new News();
        BeanUtils.copyProperties(newsReq, news);

        if (newsReq.getFileList().size() == 0) {
            String s = newsReq.getNewsPicVo().toString();
            String substring = s.substring(1, s.length() - 1);
            news.setNewsPic(substring);
            news.setId(Long.parseLong(newsReq.getId()));
            boolean b = newsService.updateById(news);
            redisConfig.getRedisTemplateByDb(0).expire("homeInfo", Duration.ZERO);
            return ajaxSuccess("200", "更新成功!");
        }
        String pic = "";
        for (HomeInfoReq homeInfoReq : newsReq.getFileList()) {
            pic = pic + homeInfoReq.getUrl() + ",";
        }
        news.setNewsPic(pic);
        news.setId(Long.parseLong(newsReq.getId()));
        boolean b = newsService.updateById(news);
        redisConfig.getRedisTemplateByDb(0).expire("homeInfo", Duration.ZERO);
        if (b) {
            return ajaxSuccess("200", "更新成功!");
        }
        return ajaxError("更新失败!");
    }

}
